import 'package:aichat_flutter_wallet/module/module_dapp/widgets/hover_status.dart';
import 'package:aichat_flutter_wallet/module/module_trade/pages/trade_transaction_details.dart';
import 'package:aichat_flutter_wallet/module_common/provider/token_info_provider.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/page_status_com.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

import '../../../module_common/router/Routers.dart';

class TradeList extends StatelessWidget {
  List<String> statusList = [
    'assets/images/wallet-trade/trade-fail.png',
    'assets/images/wallet-trade/trade-success.png',
    'assets/images/wallet-trade/trade-pending.png',
  ];
  @override
  Widget build(BuildContext context) {
    return Consumer<TokenInfoNotifier>(builder: (_, model, child) {
      return model.transactionList.length == 0
          ? PageEmptyCom(type: 'empty', paddingTop: 60.w)
          : ListView.builder(
              padding: EdgeInsets.all(0),
              itemCount: model.transactionList.length,
              itemBuilder: (_, index) {
                if (model.transactionList.length == 0) return null;
                TransactionBean transaction = model.transactionList[index];
                String date = transaction.getFormatTransactionTime();
                String from = transaction.from!;
                bool send = from.toLowerCase() == model.token.getWalletInfo()!.address.toLowerCase();
                return HoverStatus(
                  onPressed: () {
                    Navigator.of(context).push(MaterialPageRoute(
                        settings: RouteSettings(name: Routes.transactionDetails),
                        builder: (_) {
                          return TradeTransactionsPageDetails(token: model.token, transaction: transaction);
                        },
                        maintainState: false));
                  },
                  defaultColor: AppColors.c_white,
                  hoverColor: AppColors.c_F6F6F6,
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 10.w),
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              send ? 'Transfer' : 'Receive',
                              style: TextStyle(
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w500,
                                color: AppColors.c_333333,
                              ),
                            ),
                            Text(
                              (!send ? '+' : '-') +
                                  '${Utils.formatStringBalance(transaction.amount ?? '0', fractionDigits: 18, trimDecimalZero: true)}' +
                                  model.token.symbol,
                              style: TextStyle(
                                fontSize: 14.sp,
                                fontFamily: '_Din',
                                fontWeight: FontWeight.bold,
                                color: !send ? AppColors.c_3AB197 : AppColors.c_333333,
                              ),
                            )
                          ],
                        ),
                        SizedBox(
                          height: 5.w,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              Utils.formatAddress(
                                transaction.from!,
                              ),
                              style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: AppColors.c_868391),
                            ),
                            Expanded(
                                child: Center(
                              child: Image.asset(
                                'assets/images/wallet-trade/trade.png',
                                height: 6.w,
                                fit: BoxFit.fitHeight,
                              ),
                            )),
                            Text(
                              Utils.formatAddress(
                                transaction.to ?? '',
                              ),
                              style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: AppColors.c_868391),
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 7.w,
                        ),
                        Row(
                          children: [
                            Image.asset(
                              statusList[int.parse((transaction.state == 'null' || transaction.state!.isEmpty)
                                  ? '0'
                                  : transaction.state!)],
                              width: 14.w,
                              height: 14.w,
                              fit: BoxFit.fitHeight,
                            ),
                            SizedBox(
                              width: 5.w,
                            ),
                            Text(date,
                                style: TextStyle(
                                  fontSize: 12.sp,
                                  fontWeight: FontWeight.w400,
                                  color: AppColors.c_BEBDCA,
                                ))
                          ],
                        )
                      ],
                    ),
                  ),
                );
              });
    });
  }
}
